package com.swt.controller.W_controller;

import com.alibaba.fastjson.JSON;
import com.swt.domain.Club;
import com.swt.domain.User;
import com.swt.service.impl.W_impl.ClubServiceImpl;
import com.swt.service.impl.W_impl.UserServiceImpl;
import com.swt.util.TSaveImageUitl;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

@Controller
@RequestMapping("/UserController")
public class UserController {
    @Resource
    private UserServiceImpl userService;

    @Resource
    private ClubServiceImpl clubService;
    /**
     * 用户登录校验
     * * */
    @RequestMapping("/login")
    @ResponseBody
    public String login(User user, HttpServletRequest request){
        User user1=userService.checkLogin(user);
        if(user1!=null&&user1.getUserPassword().equals(user.getUserPassword())){
            request.getSession().setAttribute("USER_SESSION_KEY",user1);
            return JSON.toJSONString("true") ;
        }
        return JSON.toJSONString("false");
    }

    /**
     * 用户注册校验
     * */
    @RequestMapping("/register")
    @ResponseBody
    public String register(User user,String vcode){
        System.out.println(vcode);
        User user1=userService.checkLogin(user);
        Club club=new Club();
        club.setClubPhone(user.getUserPhone());
        Club club1=clubService.checkLogin(club);


        if(user1==null&&club1==null){
            userService.register(user);
            return JSON.toJSONString("true") ;
        }
        return JSON.toJSONString("false");
    }
    /*
    * 修改用户头像方法
    * */
    public String updateUserImage(MultipartFile myFile, HttpServletRequest request){
        /*调用方法保存用户提交的图片*/
        String fileName = TSaveImageUitl.saveImage(myFile, request);
        return fileName;

    }
}
